package com.zs.studentmanager.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zs.studentmanager.common.ResponseCodeEnum;
import com.zs.studentmanager.entity.domain.GradeClass;
import com.zs.studentmanager.entity.domain.Student;
import com.zs.studentmanager.entity.vo.PageVo;
import com.zs.studentmanager.entity.vo.StudentVo;
import com.zs.studentmanager.exception.CustomException;
import com.zs.studentmanager.mapper.StudentMapper;
import com.zs.studentmanager.service.GradeClassService;
import com.zs.studentmanager.service.StudentService;
import com.zs.studentmanager.utils.CommonUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
* @author 周小白。
* @description 针对表【s_student(学生信息表)】的数据库操作Service实现
* @createDate 2024-01-19 19:58:37
*/
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
    implements StudentService{

    private final StudentMapper studentMapper;
    private final GradeClassService gradeClassService;

    public StudentServiceImpl(StudentMapper studentMapper, GradeClassService gradeClassService) {
        this.studentMapper = studentMapper;
        this.gradeClassService = gradeClassService;
    }



    /**
     * 获取学生信息表列表
     * @param pageNum 当前页码
     * @param pageSize 每页条数
     * @param searchValue 搜索关键词
     * @return 学生信息表列表
     */
    @Override
    public PageVo<StudentVo> getStudentList(Integer pageNum, Integer pageSize, String searchValue) {
        PageVo<StudentVo> pageVo = new PageVo<>();

        // 获取总条数
        Long total = studentMapper.selectCount(null);
        pageVo.setTotal(total);

        // 分页查询
        pageNum = (pageNum - 1) * pageSize;
        List<StudentVo> studentVoList = studentMapper.getPage(pageNum, pageSize, searchValue);
        if (studentVoList == null) {
            throw new CustomException(ResponseCodeEnum.SYSTEM_ERROR);
        }
        pageVo.setList(studentVoList);


        return pageVo;
    }





    /**
     * 添加学生
     * @param studentVo 学生信息
     * @return 添加结果
     */
    @Override
    public boolean addStudent(StudentVo studentVo) {
        if (studentVo == null) {
            throw new CustomException(ResponseCodeEnum.NO_PARAM);
        }

        Student student = new Student();
        // 复制属性
        BeanUtil.copyProperties(studentVo, student);
        student.setCreateTime(LocalDateTime.now());
        long currentUserId = CommonUtils.getCurrentUserId();
        student.setCreateBy(currentUserId);

        // 获取班级id
        LambdaQueryWrapper<GradeClass> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GradeClass::getGrade, studentVo.getGrade());
        queryWrapper.eq(GradeClass::getClazz, studentVo.getClazz());
        GradeClass gradeClass = gradeClassService.getOne(queryWrapper);
        if (gradeClass == null) {
            throw new CustomException("请选择正确的班级");
        }
        // 设置班级id
        student.setGradeClassId(gradeClass.getId());

        // 添加
        int insert = studentMapper.insert(student);
        if (insert != 1) {
            throw new CustomException(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return true;
    }




    /**
     * 获取学生详情
     * @param id 学生id
     * @return 学生详情
     */
    @Override
    public StudentVo getStudentDetails(Long id) {
        if (id == null) {
            throw new CustomException(ResponseCodeEnum.NO_PARAM);
        }

        // 查询
        StudentVo studentVo = studentMapper.getStudentDetails(id);
        if (studentVo == null) {
            throw new CustomException(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return studentVo;
    }



    /**
     * 修改学生
     * @param studentVo 学生信息
     * @return 修改结果
     */
    @Override
    public boolean updateStudent(StudentVo studentVo) {
        if (studentVo == null) {
            throw new CustomException(ResponseCodeEnum.NO_PARAM);
        }

        Student student = new Student();
        // 复制属性
        BeanUtil.copyProperties(studentVo, student);
        student.setUpdateTime(LocalDateTime.now());
        long currentUserId = CommonUtils.getCurrentUserId();
        student.setUpdateBy(currentUserId);

        // 获取班级id
        LambdaQueryWrapper<GradeClass> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GradeClass::getGrade, studentVo.getGrade());
        queryWrapper.eq(GradeClass::getClazz, studentVo.getClazz());
        GradeClass gradeClass = gradeClassService.getOne(queryWrapper);
        if (gradeClass == null) {
            throw new CustomException("请选择正确的班级");
        }
        // 设置班级id
        student.setGradeClassId(gradeClass.getId());

        // 修改
        int update = studentMapper.updateById(student);
        if (update != 1) {
            throw new CustomException(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return true;
    }
}




